﻿using Core.Helper;
using Core.Modal;
using System;
using System.Collections.Generic;
using System.Data;

namespace Core.Core
{
    public class DocumentUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectid"></param>
        /// <param name="name"></param>
        /// <param name="comment"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public static int Add(int projectid,string name,string comment,string template) {
            return (int)DbHelper.ExecuteScalar($"exec proc_add_document '{projectid}','{name}','{comment}','{template}'");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectid"></param>
        /// <param name="documentid"></param>
        /// <param name="name"></param>
        /// <param name="comment"></param>
        /// <param name="template"></param>
        public static void Update(int projectid, int documentid, string name, string comment, string template) {
            DbHelper.ExecuteNonQuery($"update Document set doc_name='{name}',doc_comment='{comment}',doc_template='{template}' where project_id='{projectid}' and doc_id='{documentid}'");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectid"></param>
        /// <param name="documentid"></param>
        public static void Delete(int projectid, int documentid) {
            DbHelper.ExecuteNonQuery($"delete from Document where project_id='{projectid}' and doc_id='{documentid}'");
        }

        public static modal_Document Get(int project_id,int doc_id)
        {
            DataRow dr = DbHelper.ExecuteDataRow($"select project_id, doc_id, doc_name, doc_comment, doc_template, create_date from Document where project_id='{project_id}' and doc_id='{doc_id}'");
            if (dr != null)
            {
                modal_Document obj = new modal_Document();
                obj.create_date = Convert.ToDateTime(dr["create_date"].ToString());
                obj.project_id = Convert.ToInt32(dr["project_id"].ToString());
                obj.doc_name = dr["doc_name"].ToString();
                obj.doc_comment = dr["doc_comment"].ToString();
                obj.doc_id= Convert.ToInt32(dr["doc_id"].ToString());
                obj.doc_template = dr["doc_template"].ToString();
                return obj;
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="where"></param>
        /// <param name="orderby"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="recordcount"></param>
        /// <returns></returns>
        public static IList<modal_Document> GetList(string where, string orderby, int pageindex, int pagesize, ref int recordcount) {
            return DbHelper.PageData(orderby, pagesize, pageindex, "Document", where, ref recordcount).ToList<modal_Document>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="project_id"></param>
        /// <returns></returns>
        public static IList<modal_Document_widthTag> GetList(int project_id)
        {
            //return DbHelper.ExecuteDataSet($"select project_id, doc_id, doc_name, doc_comment, doc_template, create_date from Document where project_id='{project_id}'").Tables[0].ToList<modal_Document>();
            return DbHelper.ExecuteDataSet($"select a.project_id, a.doc_id, a.doc_name, a.doc_comment, a.doc_template, a.create_date,tag_id=ISNULL(b.tag_id,0) from Document a left join PDTrelation b on a.project_id = b.project_id where a.project_id = '{project_id}'").Tables[0].ToList<modal_Document_widthTag>();
        }

        /// <summary>
        /// 生成文档
        /// </summary>
        /// <param name="project_id"></param>
        /// <param name="docs"></param>
        /// <returns></returns>
        public static IList<modal_Preview> GetList(int project_id, string docs) {
            List<modal_Preview> list= new List<modal_Preview>();
            IList<modal_Document> list_docs = DbHelper.ExecuteDataSet($"select * from Document where project_id='{project_id}' and doc_id in ({docs})").Tables[0].ToList<modal_Document>();
            if (list_docs?.Count > 0) {
                foreach (modal_Document doc in list_docs) {
                    var modal_Preview = new modal_Preview();
                    modal_Preview.title = doc.doc_name;
                    modal_Preview.Content = RunContent(doc.doc_template, project_id, doc.doc_id);

                    list.Add(modal_Preview);
                }
            }
            return list;
        }

        /// <summary>
        /// 替换文本
        /// </summary>
        /// <param name="template"></param>
        /// <param name="project_id"></param>
        /// <param name="doc_id"></param>
        /// <returns></returns>
        private static string RunContent(string template, int project_id, int doc_id) {
            IList<modal_Tag> tags = TagUtil.GetListByDoc(project_id, doc_id);
            if (tags?.Count > 0) {
                foreach (modal_Tag tag in tags) {
                    template = template.Replace($"[#{tag.tag_name}#]",tag.tag_content);
                }
            }
            return template;
        } 
    }
}
